package com.acoreful.acf2admin.commons.mq;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import org.apache.thrift.TBase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *	RabbitMq消费者
 * @param <T>
 */
public abstract class RabbitMqConsumer<T extends TBase<?, ?>> implements MqConsumer<T> {

	private static final Logger logger = LoggerFactory.getLogger(RabbitMqConsumer.class);
	
	/**
	 * @param message
	 */
	public final void handle(byte[] message) {
		try {
			Class<T> tbaseClass = getTBaseClass();
			T tbase = tbaseClass.newInstance();
			ThriftUtils.deserialize(tbase, message);
			consume(tbase);
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
	}

	@SuppressWarnings("unchecked")
	protected Class<T> getTBaseClass() {
		Type type = getClass().getGenericSuperclass();
		Type[] genericTypes = ((ParameterizedType) type).getActualTypeArguments();
		Class<T> genericClass = (Class<T>) genericTypes[0];
		return genericClass;
	}
	
	
}
